package com.sihan.swvp.action.video;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sihan.swvp.action.BaseAction;
import com.sihan.swvp.common.CommonUtil;
import com.sihan.swvp.entity.video.VideoManage;
import com.sihan.swvp.entity.video.VideoSyn;
import com.sihan.swvp.service.sys.LogService;
import com.sihan.swvp.service.video.VideoManagerService;
import com.sihan.swvp.service.video.VideoMatchService;

@Controller
@RequestMapping("/vm")
public class VideoMatchAction extends BaseAction{
	
	@Autowired
	private VideoMatchService service;
	
	@Autowired
	VideoManagerService vmservice;
	
	
	@Autowired
	public LogService logService;
	
	
	@ResponseBody
	@RequestMapping("/getTaskList")
	public Object getTaskList(Integer page,Integer limit){
		Map<String, Object> kv = new HashMap<String, Object>();
		kv.put("task", getParameter("task"));
		kv.put("content", getParameter("content"));
		kv.put("startTime", getParameter("startTime"));
		kv.put("endTime", getParameter("endTime"));
		return pageData(service.getTaskList(kv,page,limit));
	}
	
	@ResponseBody
	@RequestMapping("/getSynPath")
	public List<Map<String, Object>> getSynPath(String mark,Integer id,Integer status,Integer bzId,Integer page,Integer limit){
		page = page == null?0:page;
		limit = limit == null?10:limit;
		Map<String, Object> kv = new HashMap<String, Object>();
		kv.put("tsaktype", getParameter("taskType"));
		kv.put("mark", mark);
		kv.put("id", id);
		kv.put("status", status);
		kv.put("bzId",bzId );
		kv.put("taskId", getParameter("taskId"));
		kv.put("videoName", getParameter("videoName"));
		kv.put("synStartTime",getParameter("synStartTime"));
		kv.put("synEndTime", getParameter("synEndTime"));
		List<Map<String, Object>> synpathlist = service.getSynPath(kv,page,limit);
		return synpathlist;
	}
	
	@ResponseBody
	@RequestMapping("/getJCandgcVideo")
	public List<Map<String, Object>> getJCandgcVideo(int bzId,String taskId){
		List<Map<String, Object>> list = service.getJCandgcVideo(taskId);
		return list;
	}
	
	@ResponseBody
	@RequestMapping("/getSynVideo")
	public List<Map<String, Object>> getSynVideo(){
		Map<String, Object> kv = new HashMap<String, Object>();
		kv.put("videoName", getParameter("videoName"));
		kv.put("synStartTime", getParameter("synStartTime"));
		kv.put("synEndTime", getParameter("synEndTime"));
		List<Map<String, Object>> list = service.getSynVideo(kv);
		
		return list;
	}

	@ResponseBody
	@RequestMapping("/getGc")
	public List<Map<String, Object>> getGc(String rwid){
		return service.getGc(rwid);
	}
	
	@ResponseBody
	@RequestMapping("/addVideoManage")
	public Object addVideoManage(Integer taskType){
		try {
			int videoId = Integer.valueOf(getParameter("video_id"));
			String taskId = getParameter("task_id");
			String mark = getParameter("mark");
			VideoManage vm = new VideoManage();
			vm.setRole_id(Integer.valueOf(getParameter("role_id")));
			vm.setTask_id(taskId);
			vm.setStage_id(Integer.valueOf(getParameter("stage_id")));
			vm.setVideo_id(videoId);
			vm.setBz_id(Integer.valueOf(getParameter("bz_id")));
			vm.setUpuser(getLoginUser().getId());
			
			int videoCounts = service.getVideoByvid(videoId,taskId);
			if(videoCounts > 0){
				return 10;
			}
			int stageCounts =  service.getStageByvid(vm);
			if(stageCounts > 0){
				return 11;
			}
			
			Integer delId = null;
			if("update".equals(mark)){
				delId = Integer.valueOf(getParameter("delId"));
			}
			service.addVideoManage(vm,mark,delId,taskType);
			VideoSyn v = vmservice.getVideoById(videoId);
			logService.addUserLog("关联视频", "视频", getLoginUser().getId(), "关联视频："+v.getName(),getRemoteAddr(),1,0);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}
	
	@ResponseBody
	@RequestMapping("/vmcounts")
	public int vmcounts(int video_id,String task_id){
		return service.getVideoByvid(video_id,task_id);
	}
	
	@ResponseBody
	@RequestMapping("/updateVm")
	public Object updateVm(Integer taskType){
		try {
			int videoId = Integer.valueOf(getParameter("video_id"));
			String taskId = getParameter("task_id");
			String mark = getParameter("mark");
			VideoManage vm = new VideoManage();
			vm.setRole_id(Integer.valueOf(getParameter("role_id")));
			vm.setTask_id(taskId);
			vm.setStage_id(Integer.valueOf(getParameter("stage_id")));
			vm.setVideo_id(videoId);
			vm.setBz_id(Integer.valueOf(getParameter("bz_id")));
			vm.setUpuser(getLoginUser().getId());
			Integer delId = CommonUtil.o2int(getParameter("delId"));
			service.addVideoManage(vm,mark,delId,taskType);
			VideoSyn v = vmservice.getVideoById(videoId);
			logService.addUserLog("修改关联视频", "视频", getLoginUser().getId(), "修改关联视频："+v.getName(),getRemoteAddr(),1,0);
			return 1;
		} catch (NumberFormatException e) {
			return -1;
		}
	}
	
	@ResponseBody
	@RequestMapping("/getBzRole")
	public List<Map<String, Object>> getBzRole(Integer bzId){
		List<Map<String, Object>> list = service.getBzRole(bzId);
		return list;
	}
	
	@ResponseBody
	@RequestMapping("/deleteVSyn")
	public int deleteVSyn(int id,String name){
		try {
			service.deleteVSyn(id);
			logService.addUserLog("删除关联视频", "视频", getLoginUser().getId(), "删除关联视频："+name,getRemoteAddr(),1,0);
			return 1;
		} catch (Exception e) {
			logService.addUserLog("删除关联视频", "视频", getLoginUser().getId(), "删除关联视频："+name,getRemoteAddr(),0,0);
			e.printStackTrace();
			return -1;
		}
	}
	
	
	
	@ResponseBody
	@RequestMapping("/getTMV")
	public List<Map<String, Object>> getTaskManageV(String taskId,String js,String gc,String stage){
		return service.getTaskManageV(taskId, js, gc,stage);
	}
	
	@ResponseBody
	@RequestMapping("/getTMVs")
	public Object getTaskManageVs(String taskId,String[] roles,String[] gcs,String bzid,int taskType){
		
		return service.getVideoDetail(taskId, roles, gcs,bzid,taskType);
	}
}
